package cn.fintecher.pangolin.service.manage.web;

import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.mysql.Resource;
import cn.fintecher.pangolin.entity.mysql.Role;
import cn.fintecher.pangolin.service.manage.repository.RoleRepository;
import cn.fintecher.pangolin.service.manage.request.*;
import cn.fintecher.pangolin.service.manage.response.RoleResponse;
import cn.fintecher.pangolin.service.manage.response.RoleUserResponse;
import cn.fintecher.pangolin.service.manage.service.RoleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @Auther: xiaqun
 * @Description: 角色相关接口
 * @Date: 9:47 2019/8/14
 */

@RestController
@RequestMapping("/api/roleController")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     */
    @ApiOperation(value = "新增角色", notes = "新增角色")
    @PostMapping("/addRole")
    public ResponseEntity<Void> addRole(@Valid @RequestBody AddRoleRequest request,
                                        @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add role,{}", request);
        roleService.addRole(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改角色
     */
    @ApiOperation(value = "修改角色", notes = "修改角色")
    @PutMapping("/modifyRole")
    public ResponseEntity<Void> modifyRole(@Valid @RequestBody ModifyRoleRequest request,
                                           @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify role,{}", request);
        roleService.modifyRole(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除角色
     */
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @DeleteMapping("/deleteRole")
    public ResponseEntity<Void> deleteRole(@RequestParam @ApiParam(value = "角色ID", required = true) String roleId,
                                           @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to delete role,{}", roleId);
        roleService.deleteRole(roleId, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 用户分配角色
     */
    @ApiOperation(value = "用户分配角色", notes = "用户分配角色")
    @PostMapping("/distributeRole")
    public ResponseEntity<Void> distributeRole(@Valid @RequestBody DistributeRoleRequest request,
                                               @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to distribute role,{}", request);
        roleService.distributeRole(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除角色下用户
     */
    @ApiOperation(value = "删除角色下用户", notes = "删除角色下用户")
    @PutMapping("/deleteRoleUser")
    public ResponseEntity<Void> deleteRoleUser(@Valid @RequestBody DeleteRoleUserRequest request,
                                               @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to delete role user,{}", request);
        roleService.deleteRoleUser(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 分配模块权限
     */
    @ApiOperation(value = "分配模块权限", notes = "分配模块权限")
    @PostMapping("/distributePermission")
    public ResponseEntity<Void> distributePermission(@Valid @RequestBody DistributePermissionRequest request,
                                                     @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to distribute permission,{}", request);
        roleService.distributePermission(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 复制角色
     */
    @ApiOperation(value = "复制角色", notes = "复制角色")
    @GetMapping("/copyRole")
    public ResponseEntity<Void> copyRole(@RequestParam @ApiParam(value = "角色ID", required = true) String roleId,
                                         @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to copy role,{}", roleId);
        roleService.copyRole(roleId, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 多条件查询角色
     */
    @ApiOperation(value = "多条件查询角色", notes = "多条件查询角色")
    @GetMapping("/findAllRole")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<RoleResponse>> findAllRole(FindRoleRequest request, Pageable pageable) {
        log.info("REST request to find role,{}", request);
        Page<RoleResponse> page = roleService.findAllRole(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 多条件查询角色下用户
     */
    @ApiOperation(value = "多条件角色下用户", notes = "多条件角色下用户")
    @GetMapping("/findUserByRole")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<RoleUserResponse>> findUserByRole(FindUserRequest request, Pageable pageable) {
        log.info("REST request to find user by role,{}", request);
        Page<RoleUserResponse> page = roleService.findUserByRole(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 查询角色下所有菜单资源
     */
    @ApiOperation(value = "查询角色下所有菜单资源", notes = "查询角色下所有菜单资源")
    @GetMapping("/findAllResource")
    public ResponseEntity<Set<Resource>> findAllResource(@RequestParam @ApiParam(value = "角色ID", required = true) String roleId) {
        log.info("REST request to find all resource,{},{}", roleId);
        Set<Resource> set = roleService.findAllResource(roleId);
        return ResponseEntity.ok().body(set);
    }

    /**
     * 获取所有角色
     */
    @ApiOperation(value = "获取所有角色", notes = "获取所有角色")
    @GetMapping("/getAllRoles")
    public ResponseEntity<List<RoleResponse>> getAllRoles() {
        return ResponseEntity.ok().body(roleService.getAllRoles());
    }

    @ApiOperation(value = "获取所有角色", notes = "获取所有角色")
    @GetMapping("/findRoles")
    public ResponseEntity<Page<RoleResponse>> findRoles(RoleRequest roleRequest, Pageable page) {
        return ResponseEntity.ok().body(roleService.findRoles(roleRequest,page));
    }


    @ApiOperation(value = "批量分配角色", notes = "批量分配角色")
    @PostMapping("/batchDistributeRole")
    public ResponseEntity<Void> batchDistributeRole(@Valid @RequestBody BatchDistributeRoleRequest request,@RequestHeader(value = "X-UserToken") String token) {
        roleService.batchDistributeRole(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询角色下所有控件资源
     */
    @ApiOperation(value = "查询角色下所有控件资源",notes = "查询角色下所有控件资源")
    @GetMapping("/findAllControls")
    public ResponseEntity<Set<Resource>> findAllControls(@RequestParam @ApiParam(value = "角色ID", required = true) String roleId) {
        log.info("REST request to find all controls,{},{}", roleId);
        Set<Resource> set = roleService.findAllControls(roleId);
        return ResponseEntity.ok().body(set);
    }
}
